package com.Tradition.controller.user;


import com.Tradition.common.check.StringCheckUtils;
import com.Tradition.common.http.HttpResult;
import com.Tradition.common.http.HttpResultEnum;
import com.Tradition.common.http.VerificationCodeEnum;
import com.Tradition.controller.BaseController;
import com.Tradition.service.UserRegService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Reg
 *
 * @author 朱笑
 * @date 2018-06-08 16:11
 */
@Api(value="用户注册",tags={"用户注册"})
@RestController
@RequestMapping(path="/reg")
public class RegController extends BaseController{
    @Autowired
    UserRegService userRegService;

    @ApiOperation(value="注册接口",notes="我就要吃鸡腿")
    @PostMapping(value="/reg.do" )
    public HttpResult reg(@ApiParam(name="用户名",value="username",required=true) String username, @ApiParam(name="密码",value="password",required=true) String password, @ApiParam(name="验证码",value="code",required=true)String code, HttpSession session) throws SQLException {
        logger.info("请求参数："+username+","+password+","+code);
        if(code==null||!session.getAttribute(VerificationCodeEnum.REG.getName()).equals(code)){
            return HttpResult.newInstance(HttpResultEnum.VERIFICATIONCODE_ERROR);
        }
        if(!StringCheckUtils.UserNameFormat(username)){
            return HttpResult.newInstance(HttpResultEnum.USERNAME_FORMAT_ERROR);
        }
        if(userRegService.checkUserName(username)>0){
            return HttpResult.newInstance(HttpResultEnum.USERNAME_EXIST);
        }
        if(!StringCheckUtils.PasswordFormat(password)){
            return HttpResult.newInstance(HttpResultEnum.PASSWORD_FORMAT_ERROR);
        }
        try {
            int result=userRegService.reg(username,password);
            return result>0?HttpResult.newInstance(HttpResultEnum.SUCCESS):HttpResult.newInstance(HttpResultEnum.FAILURE);
        }
        catch (Exception e ){
            return HttpResult.newInstance(HttpResultEnum.Exception);
        }
    }

}
